/* eslint-disable vue/no-unused-vars */
<!--发货订单-->
<!--普通订单-->
<template>
  <div class="app-container">
    <!--查询-->
    <div class="filter-container">

      <el-input v-model="listQuery.orderInfoWaybillNum" placeholder="请输入订单号" style="width: 200px;" class="filter-item" @keyup.enter.native="handleFilter" />

      <!-- 查询产品分类
      <el-select v-model="listQuery.orderInfoState" placeholder="订单状态" class="filter-item" style="width: 130px">
        <el-option label="请选订单状态" :value="-1" />
        <el-option v-for="(item,index) in calendarTypeOptions" :key="index" :label="item.display_name" :value="item.key" />
      </el-select> -->

      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        {{ $t('table.search') }} <!-- 搜索 -->
      </el-button>
    </div>

    <!--显示数据-->
    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <!-- <el-table-column :label="$t('table.date')" width="100px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.publishDate }}</span>
        </template>
      </el-table-column> -->

      <el-table-column label="订单号" min-width="100px" align="center">
        <template slot-scope="{row}">
          <span class="link-type">{{ row.orderInfoWaybillNum }}</span>
        </template>
      </el-table-column>

      <el-table-column label="支付方式" align="center">
        <template slot-scope="{row}">
          <span>{{ row.payWayInfoId==2?'微信':'支付宝' }}</span>
        </template>
      </el-table-column>

      <el-table-column label="下订单时间" align="center">
        <template slot-scope="{row}">
          <span>{{ row.orderInfoTime.substr(0,10) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="订单总金额" align="center">
        <template slot-scope="{row}">
          <span>{{ row.orderInfoSalePrice | moneyFormat }}</span>
        </template>
      </el-table-column>

      <el-table-column label="收货" width="400px">
        <template slot-scope="{row}">
          <p>收货人姓名:<span style="color:red;">{{ row.orderInfoName }}</span></p>
          <p>收货人电话:<span style="color:red;">{{ row.shoppAddreTel }}</span></p>
          <p>收货人地址:<span style="color:red;">{{ row.orderInfoAddre }}</span></p>
        </template>
      </el-table-column>

      <el-table-column label="订单状态" align="center">
        <template slot-scope="{row}">
          <span v-if="row.orderInfoState==0">已下单</span>
          <span v-if="row.orderInfoState==10">待发货</span>
          <span v-if="row.orderInfoState==20">已发货</span>
          <span v-if="row.orderInfoState==30">已完成</span>
          <span v-if="row.orderInfoState==40">订单取消</span>
        </template>
      </el-table-column>

      <el-table-column :label="$t('table.actions')" align="center" width="230" class-name="small-padding fixed-width">

        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleUpdate(row)">
            {{ $t('table.edit') }}
          </el-button>
          <!-- <el-button v-if="row.status!='deleted'" size="mini" type="danger" @click="handleDelete(row,$index)">
            加入回收站
          </el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <!--分页-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :page-sizes="[5, 10, 15, 20]"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { orderList } from '@/api/order'
import waves from '@/directive/waves' // waves directive
import { parseTime } from '@/utils'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

const calendarTypeOptions = [
  { key: 0, display_name: '已下单' },
  { key: 10, display_name: '待发货' },
  { key: 20, display_name: '已发货' },
  { key: 30, display_name: '已完成' },
  { key: 40, display_name: '订单取消' }
  // { key: 5, display_name: '恐怖' },
  // { key: 6, display_name: '科学' },
  // { key: 7, display_name: '科幻小说' },
  // { key: 8, display_name: '诗词' }
]

// arr to obj, such as { CN : "China", US : "USA" }
const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name
  return acc
}, {})

export default {
  name: 'ComplexTable',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    },
    typeFilter(type) {
      return calendarTypeKeyValue[type]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 5,
        skipCount: 0,
        maxResultCount: 5,
        importance: undefined,
        title: undefined,
        productName: undefined,
        orderInfoState: 20,
        orderInfoWaybillNum: undefined,
        sort: '+id'
      },
      importanceOptions: [1, 2, 3],
      calendarTypeOptions,
      sortOptions: [{ label: 'ID Ascending', key: '+id' }, { label: 'ID Descending', key: '-id' }],
      statusOptions: ['published', 'draft', 'deleted'],
      showReviewer: false,
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: 'Edit',
        create: 'Create'
      },
      pvData: [],
      rules: {
        // productName: [{ required: true, message: '请输入产品名称', trigger: 'change' }],
        // productSalePrice: [{ required: true, message: '请输入价格', trigger: 'change' }],
        // productWeight: [{ required: true, message: '请输入产品重量', trigger: 'blur' }]
      }
    }
  },
  created() {
    // 加载
    this.getList()
    // this.CategoryChagn()
  },
  methods: {
    getList() {
      this.listLoading = true
      // 计算跳过的条数
      this.listQuery.skipCount = (this.listQuery.page - 1) * this.listQuery.limit
      // 赋值
      this.listQuery.maxResultCount = this.listQuery.limit
      // eslint-disable-next-line no-undef
      orderList(this.listQuery).then(response => {
        console.log(response)
        this.list = response.items
        this.total = response.totalCount

        // Just to simulate the time of the request
        setTimeout(() => {
          this.listLoading = false
        }, 0.1 * 1000)
      })
    },
    sizechange(val) {
      alert(val)
      this.listQuery.maxResultCount = val
    },

    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },

    handleModifyStatus(row, status) {
      this.$message({
        message: '操作成功',
        type: 'success'
      })
      row.status = status
    },

    sortChange(data) {
    //   const { prop, order } = data
    //   if (prop === 'id') {
    //     this.sortByID(order)
    //   }
    },

    sortByID(order) {
      if (order === 'ascending') {
        this.listQuery.sort = '+id'
      } else {
        this.listQuery.sort = '-id'
      }
      this.handleFilter()
    },

    resetTemp() {
      this.temp = {
        id: undefined,
        importance: 1,
        remark: '',
        timestamp: new Date(),
        title: '',
        status: 'published',
        type: ''
      }
    },

    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },

    handleUpdate(row) {
      this.temp = Object.assign({}, row) // copy obj
      this.temp.timestamp = new Date(this.temp.timestamp)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },

    handleFetchPv(pv) {
    //   deleteArticle(pv).then(response => {
    //     this.pvData = response.data.pvData
    //     this.dialogPvVisible = true
    //   })
    },

    handleDownload() {
    //   this.downloadLoading = true
    //   import('@/vendor/Export2Excel').then(excel => {
    //     const tHeader = ['timestamp', 'title', 'type', 'importance', 'status']
    //     const filterVal = ['timestamp', 'title', 'type', 'importance', 'status']
    //     const data = this.formatJson(filterVal)
    //     excel.export_json_to_excel({
    //       header: tHeader,
    //       data,
    //       filename: 'table-list'
    //     })
    //     this.downloadLoading = false
    //   })
    },

    formatJson(filterVal) {
      return this.list.map(v => filterVal.map(j => {
        if (j === 'timestamp') {
          return parseTime(v[j])
        } else {
          return v[j]
        }
      }))
    },

    getSortClass: function(key) {
      const sort = this.listQuery.sort
      return sort === `+${key}` ? 'ascending' : 'descending'
    }

  }
}
</script>
